

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fly542">
  <meta name="keywords" content="">
  
    <meta name="description" content="解析命令行参数getopt和getopt_long在程序中一般都会用到命令行选项, 我们可以使用getopt 和getopt_long函数来解析命令行参数 getoptgetopt主要用来处理短命令行选项, 例如.&#x2F;test -v中 -v 就是一个短选项. 使用该函数需要引入头文件&lt;unistd.h&gt;, 下面是该函数的定义int getopt(int argc, char * cons">
<meta property="og:type" content="article">
<meta property="og:title" content="getopt与getopt_long使用">
<meta property="og:url" content="http://fly542.cn/2020/08/03/03%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/01c_cpp/01getopt%E4%B8%8Egetopt_long%E4%BD%BF%E7%94%A8/index.html">
<meta property="og:site_name" content="Fly542 技术沉淀">
<meta property="og:description" content="解析命令行参数getopt和getopt_long在程序中一般都会用到命令行选项, 我们可以使用getopt 和getopt_long函数来解析命令行参数 getoptgetopt主要用来处理短命令行选项, 例如.&#x2F;test -v中 -v 就是一个短选项. 使用该函数需要引入头文件&lt;unistd.h&gt;, 下面是该函数的定义int getopt(int argc, char * cons">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-03T07:00:25.000Z">
<meta property="article:modified_time" content="2022-12-30T03:33:01.328Z">
<meta property="article:author" content="Fly542">
<meta property="article:tag" content="c++">
<meta property="article:tag" content="c">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>getopt与getopt_long使用 - Fly542 技术沉淀</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"fly542.cn","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fly542 技术沉淀</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="getopt与getopt_long使用">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-08-03 15:00" pubdate>
        2020年8月3日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      5.9k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      50 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">getopt与getopt_long使用</h1>
            
            <div class="markdown-body">
              <h1 id="解析命令行参数getopt和getopt-long"><a href="#解析命令行参数getopt和getopt-long" class="headerlink" title="解析命令行参数getopt和getopt_long"></a>解析命令行参数getopt和getopt_long</h1><p>在程序中一般都会用到命令行选项, 我们可以使用<strong>getopt 和getopt_long</strong>函数来解析命令行参数</p>
<h2 id="getopt"><a href="#getopt" class="headerlink" title="getopt"></a>getopt</h2><p>getopt主要用来处理短命令行选项, 例如<code>./test -v</code>中 -v 就是一个短选项. 使用该函数需要引入头文件&lt;unistd.h&gt;, 下面是该函数的定义<br><code>int getopt(int argc, char * const argv[], const char * optstring);</code></p>
<p>其中 <code>argc</code> 和 <code>argv</code> 是 <code>main</code> 函数中的传递的参数个数和内容, <code>optstring</code> 用来指定可以处理哪些选项, 下面是 <code>optstring</code> 的一个示例:<br><code>&quot;a:bc&quot;</code></p>
<p>该示例表明程序可以接受3个选项: -a -b -c, 其中 a 后面的 :表示该选项后面要跟一个参数, 即如 -a text的形式, 选项后面跟的参数会被保存到 optarg 变量中. 下面是一个使用示例:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></div></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;unistd.h&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> **argv)</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> ch;<br>    <span class="hljs-keyword">while</span>((ch = <span class="hljs-built_in">getopt</span>(argc, argv, <span class="hljs-string">&quot;a:b&quot;</span>)) != <span class="hljs-number">-1</span>) &#123;<br>        <span class="hljs-built_in"><span class="hljs-keyword">switch</span></span>(ch) &#123;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;a&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;option a: %s\n&quot;</span>, optarg);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;b&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;option b \n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;?&#x27;</span>: <span class="hljs-comment">// 输入未定义的选项, 都会将该选项的值变为 ?</span><br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;unknown option \n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">default</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;default \n&quot;</span>);<br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>执行 <code>./test -a aa -b -c</code> 输出结果如下:</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs pgsql"><span class="hljs-keyword">option</span> a: aa<br><span class="hljs-keyword">option</span> b <br><span class="hljs-type">unknown</span> <span class="hljs-keyword">option</span><br></code></pre></td></tr></table></figure>

<h2 id="getopt-long"><a href="#getopt-long" class="headerlink" title="getopt_long"></a>getopt_long</h2><p><code>getopt_long</code>支持长选项的命令行解析, 所为长选项就是诸如–help的形式, 使用该函数, 需要引入&lt;getopt.h&gt;下面是函数原型:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;getopt.h&gt;</span></span><br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getopt_long</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, </span></span><br><span class="hljs-params"><span class="hljs-function">                <span class="hljs-keyword">char</span> * <span class="hljs-keyword">const</span> argv[],</span></span><br><span class="hljs-params"><span class="hljs-function">                <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *optstring,</span></span><br><span class="hljs-params"><span class="hljs-function">                <span class="hljs-keyword">const</span> struct option *longopts,</span></span><br><span class="hljs-params"><span class="hljs-function">                <span class="hljs-keyword">int</span> *longindex)</span></span>;<br><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">getopt_long_only</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc,</span></span><br><span class="hljs-params"><span class="hljs-function">                    <span class="hljs-keyword">char</span> * <span class="hljs-keyword">const</span> argv[],</span></span><br><span class="hljs-params"><span class="hljs-function">                    <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *optstring,</span></span><br><span class="hljs-params"><span class="hljs-function">                    <span class="hljs-keyword">const</span> struct option *longopts,</span></span><br><span class="hljs-params"><span class="hljs-function">                    <span class="hljs-keyword">int</span> *longindex)</span></span>;<br></code></pre></td></tr></table></figure>
<p>其中 argc , argv , optstring 和getopt中的含义一样, 下面解释一下longopts 和longindex</p>
<h3 id="longopts"><a href="#longopts" class="headerlink" title="longopts"></a>longopts</h3><p><code>longopts</code> 指向一个 <code>struct option</code> 的数组, 下面是<code>option</code>的定义:</p>
<figure class="highlight sml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs sml"><span class="hljs-keyword">struct</span> <span class="hljs-built_in">option</span> &#123;<br>    const <span class="hljs-built_in">char</span> *name;<br>    <span class="hljs-built_in">int</span>         has_arg;<br>    <span class="hljs-built_in">int</span>        *flag;<br>    <span class="hljs-built_in">int</span>         <span class="hljs-keyword">val</span>;<br>&#125;;<br></code></pre></td></tr></table></figure>
<p>下面是各字段的含义<br>    name - 长选项的名称, 例如 help<br>    has_arg - 是否带参数, 0 不带参数, 1 必须带参数, 2 参数可选<br>    flag - 指定长选项如何返回结果, 如果flag为NULL, getopt_long() 会返回val. 如果flag不为NULL, getopt_long会返回0, 并且将val的值存储到flag中<br>    val - 将要被getopt_long返回或者存储到flag指向的变量中的值</p>
<p>下面是 <code>longopts</code> 的一个示例</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">option</span> <span class="hljs-title">opts</span>[] =</span> &#123;<br>    &#123;<span class="hljs-string">&quot;version&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;v&#x27;</span>&#125;,<br>    &#123;<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-number">1</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;n&#x27;</span>&#125;,<br>    &#123;<span class="hljs-string">&quot;help&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;h&#x27;</span>&#125;<br>&#125;;<br></code></pre></td></tr></table></figure>
<p>我们来看<code>&#123;&quot;version&quot;, 0, NULL, &#39;v&#39;&#125;</code>, <code>version</code> 即为长选项的名称, 即按如下形式<code>--version</code>, 0 表示该选项后面不带参数, NULL 表示直接将v返回(字符v在ascii码中对应的数值), 即在使用getopt_long遍历到该条选项时, getopt_long 返回值为字符v对应的ascii码值.</p>
<h3 id="longindex"><a href="#longindex" class="headerlink" title="longindex"></a>longindex</h3><p><code>longindex</code> 表示长选项在<code>longopts</code> 中的位置, 例如在上面的示例中, <code>version</code> 对应的 <code>longindex</code> 为0, <code>name</code>对应的 <code>longindex</code> 为1, <code>help</code> 对应的 <code>longindex</code> 为2, 该项主要用于调试, 一般设为 NULL 即可.</p>
<p>下面是一个使用示例:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">use_getpot_long</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span> </span>&#123;<br>    <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *optstring = <span class="hljs-string">&quot;vn:h&quot;</span>;<br>    <span class="hljs-keyword">int</span> c;<br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">option</span> <span class="hljs-title">opts</span>[] =</span> &#123;<br>        &#123;<span class="hljs-string">&quot;version&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;v&#x27;</span>&#125;,<br>        &#123;<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-number">1</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;n&#x27;</span>&#125;,<br>        &#123;<span class="hljs-string">&quot;help&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;h&#x27;</span>&#125;<br>    &#125;;<br><br>    <span class="hljs-keyword">while</span>((c = <span class="hljs-built_in">getopt_long</span>(argc, argv, optstring, opts, <span class="hljs-literal">NULL</span>)) != <span class="hljs-number">-1</span>) &#123;<br>        <span class="hljs-built_in"><span class="hljs-keyword">switch</span></span>(c) &#123;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;n&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;username is %s\n&quot;</span>, optarg);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;v&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;version is 0.0.1\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;h&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;this is help\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;?&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;unknown option\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> :<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;the return val is 0\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">default</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;------\n&quot;</span>);<br><br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>然后我们运行程序 ./test –name aaaa –version –help –haha, 下面是运行结果:</p>
<div class="code-wrapper"><pre><code class="hljs">username is aaaa
version is 0.0.1
this is help
./test: unrecognized option &#39;--haha&#39;
unknown option
</code></pre></div>
<p>当然我们也可以使用短选项 ./test -n aaaa -v -h<br>下面我们对程序做一下修改, 这一次将 struct option 中的 flag 和 longindex 设为具体的值</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">use_getpot_long2</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span> </span>&#123;<br>    <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *optstring = <span class="hljs-string">&quot;vn:h&quot;</span>;<br>    <span class="hljs-keyword">int</span> c;<br><br>    <span class="hljs-keyword">int</span> f_v = <span class="hljs-number">-1</span>, f_n = <span class="hljs-number">-1</span>, f_h = <span class="hljs-number">-1</span>, opt_index = <span class="hljs-number">-1</span>; <br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">option</span> <span class="hljs-title">opts</span>[] =</span> &#123;<br>        &#123;<span class="hljs-string">&quot;version&quot;</span>, <span class="hljs-number">0</span>, &amp;f_v, <span class="hljs-string">&#x27;v&#x27;</span>&#125;,<br>        &#123;<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-number">1</span>, &amp;f_n, <span class="hljs-string">&#x27;n&#x27;</span>&#125;,<br>        &#123;<span class="hljs-string">&quot;help&quot;</span>, <span class="hljs-number">0</span>, &amp;f_h, <span class="hljs-string">&#x27;h&#x27;</span>&#125;<br>    &#125;;<br><br>    <span class="hljs-keyword">while</span>((c = <span class="hljs-built_in">getopt_long</span>(argc, argv, optstring, opts, &amp;opt_index)) != <span class="hljs-number">-1</span>) &#123;<br>        <span class="hljs-built_in"><span class="hljs-keyword">switch</span></span>(c) &#123;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;n&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;username is %s\n&quot;</span>, optarg);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;v&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;version is 0.0.1\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;h&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;this is help\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;?&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;unknown option\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> :<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;f_v is %d \n&quot;</span>, f_v);<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;f_n is %d \n&quot;</span>, f_n);<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;f_h is %d \n&quot;</span>, f_h);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">default</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;------\n&quot;</span>);<br>        &#125;<br>        <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;opt_index is %d\n\n&quot;</span>, opt_index);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>运行程序: ./test –name bbbb –version –help , 下面是运行结果:</p>
<figure class="highlight actionscript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs actionscript">f_v <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span> <br>f_n <span class="hljs-keyword">is</span> <span class="hljs-number">110</span> <br>f_h <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span> <br>opt_index <span class="hljs-keyword">is</span> <span class="hljs-number">1</span><br><br>f_v <span class="hljs-keyword">is</span> <span class="hljs-number">118</span> <br>f_n <span class="hljs-keyword">is</span> <span class="hljs-number">110</span> <br>f_h <span class="hljs-keyword">is</span> <span class="hljs-number">-1</span> <br>opt_index <span class="hljs-keyword">is</span> <span class="hljs-number">0</span><br><br>f_v <span class="hljs-keyword">is</span> <span class="hljs-number">118</span> <br>f_n <span class="hljs-keyword">is</span> <span class="hljs-number">110</span> <br>f_h <span class="hljs-keyword">is</span> <span class="hljs-number">104</span> <br>opt_index <span class="hljs-keyword">is</span> <span class="hljs-number">2</span><br></code></pre></td></tr></table></figure>
<p>我们可以看到当给 flag 指定具体的指针之后, getopt_long 会返回0, 因此会去执行case 0, 并且 val 的值赋给了 flag 指向的变量. 下面我们用短选项执行一下程序 ./test -n bbbb -v -h, 下面是运行结果</p>
<figure class="highlight d"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs d">username <span class="hljs-keyword">is</span> bbbb<br>opt_index <span class="hljs-keyword">is</span> -<span class="hljs-number">1</span><br><br><span class="hljs-keyword">version</span> <span class="hljs-keyword">is</span> <span class="hljs-number">0.0</span><span class="hljs-number">.1</span><br>opt_index <span class="hljs-keyword">is</span> -<span class="hljs-number">1</span><br><br><span class="hljs-keyword">this</span> <span class="hljs-keyword">is</span> help<br>opt_index <span class="hljs-keyword">is</span> -<span class="hljs-number">1</span><br></code></pre></td></tr></table></figure>
<p>我们看到使用短选项的时候 getopt_long 就相当于 getopt , flag 和 longindex都不起作用了.</p>
<p>getopt_long 和 getopt_long_only<br>下面解释一下 getopt_long 和 getopt_long_only的区别, 首先用下列选项运行一下 use_getopt_long ./test -name zhangjkai -version -help , 下面是输出结果:</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs pgsql">username <span class="hljs-keyword">is</span> ame<br><span class="hljs-keyword">version</span> <span class="hljs-keyword">is</span> <span class="hljs-number">0.0</span><span class="hljs-number">.1</span><br>./test: invalid <span class="hljs-keyword">option</span> <span class="hljs-comment">-- &#x27;e&#x27;</span><br><span class="hljs-type">unknown</span> <span class="hljs-keyword">option</span><br>./test: invalid <span class="hljs-keyword">option</span> <span class="hljs-comment">-- &#x27;r&#x27;</span><br><span class="hljs-type">unknown</span> <span class="hljs-keyword">option</span><br>./test: invalid <span class="hljs-keyword">option</span> <span class="hljs-comment">-- &#x27;s&#x27;</span><br><span class="hljs-type">unknown</span> <span class="hljs-keyword">option</span><br>./test: invalid <span class="hljs-keyword">option</span> <span class="hljs-comment">-- &#x27;i&#x27;</span><br><span class="hljs-type">unknown</span> <span class="hljs-keyword">option</span><br>./test: invalid <span class="hljs-keyword">option</span> <span class="hljs-comment">-- &#x27;o&#x27;</span><br><span class="hljs-type">unknown</span> <span class="hljs-keyword">option</span><br>username <span class="hljs-keyword">is</span> -help<br></code></pre></td></tr></table></figure>

<p>我们看到使用短选项标识符 - 指向长选项时, 程序还是会按短选项来处理, 即一个字符一个字符的解析. 下面我们将 use_getopt_long 做一下更改, 即将 getopt_long 改为 getopt_long_only , 如下所示:</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs cpp"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">use_getpot_long3</span><span class="hljs-params">(<span class="hljs-keyword">int</span> argc, <span class="hljs-keyword">char</span> *argv[])</span> </span>&#123;<br>    <span class="hljs-keyword">const</span> <span class="hljs-keyword">char</span> *optstring = <span class="hljs-string">&quot;vn:h&quot;</span>;<br>    <span class="hljs-keyword">int</span> c;<br>    <span class="hljs-class"><span class="hljs-keyword">struct</span> <span class="hljs-title">option</span> <span class="hljs-title">opts</span>[] =</span> &#123;<br>        &#123;<span class="hljs-string">&quot;version&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;v&#x27;</span>&#125;,<br>        &#123;<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-number">1</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;n&#x27;</span>&#125;,<br>        &#123;<span class="hljs-string">&quot;help&quot;</span>, <span class="hljs-number">0</span>, <span class="hljs-literal">NULL</span>, <span class="hljs-string">&#x27;h&#x27;</span>&#125;<br>    &#125;;<br><br>    <span class="hljs-keyword">while</span>((c = <span class="hljs-built_in">getopt_long_only</span>(argc, argv, optstring, opts, <span class="hljs-literal">NULL</span>)) != <span class="hljs-number">-1</span>) &#123;<br>        <span class="hljs-built_in"><span class="hljs-keyword">switch</span></span>(c) &#123;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;n&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;username is %s\n&quot;</span>, optarg);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;v&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;version is 0.0.1\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;h&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;this is help\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-string">&#x27;?&#x27;</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;unknown option\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-number">0</span> :<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;the return val is 0\n&quot;</span>);<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">default</span>:<br>                <span class="hljs-built_in">printf</span>(<span class="hljs-string">&quot;------\n&quot;</span>);<br><br>        &#125;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<p>下面再运行程序 <code>./test -name cccc -version -help </code>, 下面是运行结果:</p>
<figure class="highlight d"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs d">username <span class="hljs-keyword">is</span> cccc<br><span class="hljs-keyword">version</span> <span class="hljs-keyword">is</span> <span class="hljs-number">0.0</span><span class="hljs-number">.1</span><br><span class="hljs-keyword">this</span> <span class="hljs-keyword">is</span> help<br></code></pre></td></tr></table></figure>

<p>即使用 <code>getopt_long_only</code> 时, - 和 –都可以作用于长选项, 而使用 <code>getopt_only</code> 时, 只有 –可以作用于长选项.</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/">软件开发</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/c/">c++</a>
                    
                      <a class="hover-with-bg" href="/tags/c/">c</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/08/13/03%E8%BD%AF%E4%BB%B6%E5%BC%80%E5%8F%91/03linux/05VIRT%E4%B8%8ERES%E5%8D%A0%E7%94%A8%E5%88%86%E6%9E%90/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">VIRT 与 RSE 占用分析</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/08/02/01%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/01%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">
                        <span class="hidden-mobile">设计模式的六大原则</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
